package dongze.com.ckdj.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dongze.com.ckdj.CKDJApplication;
import dongze.com.ckdj.R;
import dongze.com.ckdj.dagger.DataStore;
import dongze.com.ckdj.dagger.component.AppComponent;
import dongze.com.ckdj.service.ImageUploadService;
import dongze.com.ckdj.util.MyLog;

/**
 * Created by Administrator on 2017/7/18.
 */
public abstract class BaseActivity extends AppCompatActivity {
    private static final String TAG = BaseActivity.class.getSimpleName();
    private String[] permissions = new String[]{
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.INTERNET,
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECEIVE_BOOT_COMPLETED,
            Manifest.permission.READ_PHONE_STATE,
    };
    TextView mTitle;
    TextView mLeft;
    TextView mRight;

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyLog.d(getClass().getSimpleName(), "onCreate: ");
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setCustomView(R.layout.action_bar);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//        actionBar.setDisplayHomeAsUpEnabled(true);
//        actionBar.setDisplayShowHomeEnabled(true);
        mTitle = (TextView) actionBar.getCustomView().findViewById(R.id.tv_title);
        mLeft = (TextView) actionBar.getCustomView().findViewById(R.id.tv_left);
        mLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mRight = (TextView) actionBar.getCustomView().findViewById(R.id.tv_right);
        setActionBarButton();
        mTitle.setText(BaseActivity.this.getTitle());

//        ActivityCompat.requestPermissions(this, permissions, 0);

        List<String> arr = new ArrayList<>(permissions.length);

        for (String permission :
                permissions) {
            if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED)
                arr.add(permission);
        }

        if (arr.size() > 0) {
            String[] denied = new String[arr.size()];
            arr.toArray(denied);
            ActivityCompat.requestPermissions(this, denied, 0);
        }

        getAppCompenent().inject(this);
        ((CKDJApplication) getApplication()).addActivity(this);
    }

    @Override
    protected void onResume() {
        MyLog.d(getClass().getSimpleName(), "onResume: ");
        mTitle.setText(BaseActivity.this.getTitle());
        super.onResume();
    }

    @Override
    protected void onPause() {
        MyLog.d(getClass().getSimpleName(), "onPause: ");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        MyLog.d(getClass().getSimpleName(), "onDestroy: ");
        ((CKDJApplication) getApplication()).removeAxctivity(this);
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        MyLog.d(getClass().getSimpleName(), "onSaveInstanceState: ");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        MyLog.d(getClass().getSimpleName(), "onRestoreInstanceState: ");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 0 && permissions.length != 0) {
            startService(new Intent(this, ImageUploadService.class));
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        if (item.getItemId() == android.R.id.home) {
//            finish();
//            return true;
//        }
//
//        return super.onOptionsItemSelected(item);
//    }

    public AppComponent getAppCompenent() {
        return ((CKDJApplication) getApplication()).getAppComponent();
    }

    public <T> void addComponent(Class<T> clazz, T component) {
        ((CKDJApplication) getApplication()).addComponent(clazz, component);
    }

    public <T> T getComponent(Class<T> clazz) {
        return ((CKDJApplication) getApplication()).getComponent(clazz);
    }

    /**
     * 设置返回和保存显示状态和响应
     */
    protected void setActionBarButton() {
        mLeft.setVisibility(View.VISIBLE);
        mRight.setVisibility(View.INVISIBLE);
    }

}
